package booksys.defer;

import booksys.common.CurrentUser;
import booksys.common.Utils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

//业务方法
public class DeferService {

    public List<Bookdefer> getNewBookdeferList(CurrentUser currentUser) {

        SqlSession sess = Utils.openSession();
        try {
            DeferDao dao = sess.getMapper(DeferDao.class);
            List<Bookdefer> deferList = dao.findNewBookdeferList(currentUser.getUserId());//拿到集合
            sess.commit();//提交事务

            return deferList;
        } catch (Exception e) {
            sess.rollback();//出现异常回滚事务
            e.printStackTrace();
            throw new RuntimeException("获取申请列表失败！", e);

        } finally {
            sess.close();//关闭会话,否则其他用户无法连接数据库
        }
    }

    public void permitBookdefer(DeferDto dto) {
        SqlSession sess = Utils.openSession();
        try {
            DeferDao dao = sess.getMapper(DeferDao.class);
            dao.permitBookdefer(dto.getBd_id());
            dao.updatebbExtime(dto);
            dao.updatebdExtime(dto);
            sess.commit();
        } catch (Exception e) {
            sess.rollback();
            throw new RuntimeException("缓期申请结果处理失败！", e);
        } finally {
            sess.close();
        }
    }

    public void banBookdefer(DeferDto dto) {
        SqlSession sess = Utils.openSession();
        try {
            DeferDao dao = sess.getMapper(DeferDao.class);
            dao.banBookdefer(dto.getBd_id());
            sess.commit();
        } catch (Exception e) {
            sess.rollback();
            throw new RuntimeException("缓期申请结果处理失败！", e);
        } finally {
            sess.close();
        }
    }

    public List<Bookdefer> getHistBookdeferList(CurrentUser currentUser) {

        SqlSession sess = Utils.openSession();
        try {
            DeferDao dao = sess.getMapper(DeferDao.class);
            List<Bookdefer> deferList = dao.findHistBookdeferList(currentUser.getUserId());//拿到集合
            sess.commit();//提交事务

            return deferList;
        } catch (Exception e) {
            sess.rollback();//出现异常回滚事务
            e.printStackTrace();
            throw new RuntimeException("获取申请列表失败！", e);
        } finally {
            sess.close();//关闭会话,否则其他用户无法连接数据库
        }
    }

}
